﻿using System;
using System.IO;

namespace _05.MaxSumOf2x2Elements
{
    class MaxSum
    {
        static void Main()
        {
            using (StreamReader sr = new StreamReader("SquareMatrix.txt"))
            {
                int matrixSize = int.Parse(sr.ReadLine());
                int[,] squareMatrix = new int[matrixSize, matrixSize];
                string[] separators = new string[] { " ", "\t" };

                for (int i = 0; i < squareMatrix.GetLength(0); i++)
                {
                    string line = sr.ReadLine();
                    string[] numberArray = line.Split(separators, StringSplitOptions.RemoveEmptyEntries);
                    for (int j = 0; j < squareMatrix.GetLength(1); j++)
                    {
                        squareMatrix[i, j] = int.Parse(numberArray[j]);
                    }
                }

                int maxSum = int.MinValue;
                int maxSumRowIndex = 0;
                int maxSumColIndex = 0;

                for (int row = 0; row < squareMatrix.GetLength(0) - 1; row++)
                {
                    for (int col = 0; col < squareMatrix.GetLength(1) - 1; col++)
                    {
                        int sum = 0;
                        for (int subRow = row; subRow < row + 2; subRow++)
                        {
                            for (int subCol = col; subCol < col + 2; subCol++)
                            {
                                sum += squareMatrix[subRow, subCol];
                            }
                        }
                        if (sum > maxSum)
                        {
                            maxSum = sum;
                            maxSumRowIndex = row;
                            maxSumColIndex = col;
                        }
                    }
                }

                Console.WriteLine("Maximal sum is {0}.", maxSum);
                Console.WriteLine("Matrix index: [{0},{1}].", maxSumRowIndex, maxSumColIndex);
                Console.WriteLine("The sub matrix is:");
                for (int subRow = maxSumRowIndex; subRow < maxSumRowIndex + 2; subRow++)
                {
                    for (int subCol = maxSumColIndex; subCol < maxSumColIndex + 2; subCol++)
                    {
                        Console.Write("{0,4}", squareMatrix[subRow, subCol]);
                    }
                    Console.WriteLine();
                }
            }
        }
    }
}
